package com.wzt.lianfirecontrol.adapter.home.system.jiance;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.home.system.SysDeviceModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class SysJianCeDeviceAdapter extends BaseRecyclerAdapter<SysDeviceModel> {
    private Context context;

    private boolean needSetTopMargin = false;

    private String planId;

    private CallBack callBack;

    public String getPlanId() {
        return planId;
    }

    public void setPlanId(String planId) {
        this.planId = planId;
    }

    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public SysJianCeDeviceAdapter(Context context, CallBack callBack) {
        this.context = context;
        this.callBack = callBack;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_info_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, SysDeviceModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_title;
        private TextView tv_status;
        private TextView tv_flag;
        private ImageView iv_show_goto;
        private LinearLayout ll_location;
        private ImageView iv_location;
        private TextView tv_location;
        private TextView tv_nfc_flag;
        private ImageView iv_videos;

        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.tv_flag = itemView.findViewById(R.id.tv_flag);
        myViewHolder.iv_show_goto = itemView.findViewById(R.id.iv_show_goto);
        myViewHolder.ll_location = itemView.findViewById(R.id.ll_location);
        myViewHolder.iv_location = itemView.findViewById(R.id.iv_location);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.tv_nfc_flag = itemView.findViewById(R.id.tv_nfc_flag);
        myViewHolder.iv_videos = itemView.findViewById(R.id.iv_videos);

    }

    private void setItemData(MyViewHolder viewHolder, SysDeviceModel data, int position) {
        viewHolder.iv_show_goto.setVisibility(View.VISIBLE);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position && needSetTopMargin){
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15));
        }else {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
        }
        if (StringUtils.isEmpty(data.getName())){
            viewHolder.tv_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getName());
        }

        viewHolder.tv_status.setVisibility(View.GONE);
        viewHolder.iv_show_goto.setImageResource(R.mipmap.goto_right);
        viewHolder.tv_nfc_flag.setVisibility(View.GONE);
        if (!StringUtils.isEmpty(data.getEquVideoIds())){
            viewHolder.iv_videos.setVisibility(View.VISIBLE);
            viewHolder.iv_videos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取监控设备
                    callBack.getVideosList(data.getEquVideoIds());
                }
            });
        }else {
            viewHolder.iv_videos.setVisibility(View.GONE);
        }


        if (StringUtils.isEmpty(data.getRunStatus()) || "0".equals(data.getRunStatus())){
            viewHolder.tv_flag.setVisibility(View.GONE);
        }else {
            viewHolder.tv_flag.setVisibility(View.VISIBLE);
            viewHolder.tv_flag.setText(Utils.changeStatusIdToStr(data.getRunStatus()));
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(Color.parseColor(Utils.changeStatusIdToColor(data.getRunStatus())));
            gradientDrawable.setCornerRadius(Utils.dip2px(context, 10));
            viewHolder.tv_flag.setBackground(gradientDrawable);
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isEmpty(data.getAddress())){
            viewHolder.iv_location.setVisibility(View.VISIBLE);
            viewHolder.ll_location.setVisibility(View.VISIBLE);
            if (!StringUtils.isEmpty(data.getBuildingName())){
                stringBuilder.append(data.getBuildingName());
            }
            if (!StringUtils.isEmpty(data.getFloorName())){
                stringBuilder.append(data.getFloorName());
            }
//            if (!StringUtils.isEmpty(data.getAddress())){
//                stringBuilder.append(data.getAddress());
//            }
            viewHolder.tv_location.setText(stringBuilder.toString());
        }else {

            viewHolder.ll_location.setVisibility(View.VISIBLE);
            viewHolder.iv_location.setVisibility(View.VISIBLE);

            if (!StringUtils.isEmpty(data.getBuildingName())){
                stringBuilder.append(data.getBuildingName());
            }
            if (!StringUtils.isEmpty(data.getFloorName())){
                stringBuilder.append(data.getFloorName());
            }
            if (!StringUtils.isEmpty(data.getAddress())){
                stringBuilder.append(data.getAddress());
            }
            viewHolder.tv_location.setText(stringBuilder.toString());
        }
    }

    public interface CallBack{
        void getVideosList(String videoIds);
    }
    
}
